package com.wuchao.servlet143;

import com.alibaba.fastjson.JSON;
import com.wuchao.entity.Pojo;
import com.wuchao.service.serviceimpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/26 16:25
 */
@WebServlet(name = "ServletAjax",urlPatterns = "/ServletAjax")
public class ServletAjax extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();

        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();
        String op =request.getParameter("op");
        if(op.equals("login")){
            String text = request.getParameter("text");
            int loading = new ServiceImpl().isLoading("select count(1) from t143 where name=?", text);
            System.out.println(loading);
            if(loading>0){
                out.println("y");//异步就是我送你回去，想不到吧，曾经的我默默无闻。现在最难的还是他出马
            }else{
                out.println("n");
            }


        }else  if(op.equals("login1")){
            String text = request.getParameter("text");
            int loading = new ServiceImpl().isLoading("select count(1) from t143 where name=?", text);
            System.out.println(loading);
            if(loading>0){
                out.println("y");//异步就是我送你回去，想不到吧，曾经的我默默无闻。现在最难的还是他出马
            }else{
                out.println("n");
            }

        }else  if(op.equals("find")){
            String text = request.getParameter("user");
            List<Pojo> poJo = new ServiceImpl().
                    findPoJo("select * from t143 where name like ?", "%" + text + "%");
            String s = JSON.toJSONString(poJo);
            System.out.println(s);
            out.println(s);

            //咱们国产软甲alibaba fastjson 快速序列化
           // var jarr3=[{"name":"超哥1","age":32},{"name":"超哥2","age":32},{"name":"超哥3","age":32}];
           //拼接 json回去
          /*  StringBuilder sb=new StringBuilder("[");
            for(int i=0;i<poJo.size();i++){
                String json="{\"id\":\""+poJo.get(i).getId()+"\",\"name\":\""+poJo.get(i).getName()+"\"}";
               sb.append(json);
                if(i<poJo.size()-1){
                    sb.append(",");
                }
            }
            sb.append("]");
            System.out.println(sb);
            out.println(sb);*/


        }


        out.flush();
        out.close();
    }
}
